package com.only4play.productcenter.domain.product;

import com.only4play.codegen.processor.api.*;
import com.only4play.codegen.processor.controller.GenController;
import com.only4play.codegen.processor.creator.GenCreator;
import com.only4play.codegen.processor.creator.IgnoreCreator;
import com.only4play.codegen.processor.mapper.GenMapper;
import com.only4play.codegen.processor.query.GenQuery;
import com.only4play.codegen.processor.repository.GenRepository;
import com.only4play.codegen.processor.service.GenService;
import com.only4play.codegen.processor.service.GenServiceImpl;
import com.only4play.codegen.processor.updater.GenUpdater;
import com.only4play.codegen.processor.updater.IgnoreUpdater;
import com.only4play.codegen.processor.vo.GenVo;
import com.only4play.common.annotation.FieldDesc;
import com.only4play.common.constants.ValidStatus;
import com.only4play.jpa.converter.ValidStatusConverter;
import com.only4play.jpa.support.BaseJpaAggregate;
import com.only4play.productcenter.infrastructure.constants.Constants;
import lombok.Data;
import org.graalvm.nativebridge.In;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Objects;

@GenVo(pkgName = "com.only4play.productcenter.domain.product.vo")
@GenCreator(pkgName = "com.only4play.productcenter.domain.product.creator")
@GenUpdater(pkgName = "com.only4play.productcenter.domain.product.updater")
@GenRepository(pkgName = "com.only4play.productcenter.domain.product.repository")
@GenService(pkgName = "com.only4play.productcenter.domain.product.service")
@GenServiceImpl(pkgName = "com.only4play.productcenter.domain.product.service")
@GenQuery(pkgName = "com.only4play.productcenter.domain.product.query")
@GenMapper(pkgName = "com.only4play.productcenter.domain.product.mapper")
@GenController(pkgName = "com.only4play.productcenter.controller")
@GenCreateRequest(pkgName = "com.only4play.productcenter.api.product.request", sourcePath = Constants.GEN_API_SOURCE)
@GenUpdateRequest(pkgName = "com.only4play.productcenter.api.product.request", sourcePath = Constants.GEN_API_SOURCE)
@GenQueryRequest(pkgName = "com.only4play.productcenter.api.product.request", sourcePath = Constants.GEN_API_SOURCE)
@GenResponse(pkgName = "com.only4play.productcenter.api.product.response", sourcePath = Constants.GEN_API_SOURCE)
@GenFeign(pkgName = "com.only4play.productcenter.api.product.service", sourcePath = Constants.GEN_API_SOURCE, serverName = "product-srv")
@Entity
@Table(name = "product_category")
@Data
public class ProductCategory extends BaseJpaAggregate {

    @Column(columnDefinition = "COMMENT '父id'")
    @FieldDesc(name = "父类id")
    private Long pid;

    @FieldDesc(name = "分类名称")
    private String name;

    @FieldDesc(name = "创建人")
    private String createUser;

    @Column(columnDefinition = "COMMENT '排序号'")
    @FieldDesc(name = "排序号")
    private Integer sortNum;


    @Convert(converter = ValidStatusConverter.class)
    @IgnoreUpdater
    @IgnoreCreator
    private ValidStatus validStatus;

    /**
     * 初始化方法
     *
     * @param pid 上级标识，如果为null，则默认设置为根节点标识
     * @param sortNum 排序编号，如果为null，则默认设置为预设的排序编号
     * 该方法不返回任何内容。
     */
    public void init(Long pid, Integer sortNum) {
        // 根据pid参数的有无，设置上级标识
        if(Objects.isNull(pid)){
            setPid(Constants.ROOT_ID);
        }else{
            setPid(pid);
        }
        // 根据sortNum参数的有无，设置排序编号
        if(Objects.isNull(sortNum)){
            setSortNum(Constants.DEFAULT_SORT_NUM);
        }else{
            setSortNum(sortNum);
        }
        // 设置有效状态为有效
        setValidStatus(ValidStatus.VALID);
    }

    public void valid() {
        setValidStatus(ValidStatus.VALID);
    }

    public void invalid() {
        setValidStatus(ValidStatus.INVALID);
    }
}